/*
 * @Author: '浪川' '1214391613@qq.com'
 * @Date: 2025-07-02 15:01:09
 * @LastEditors: '浪川' '1214391613@qq.com'
 * @LastEditTime: 2025-08-09 10:09:26
 * @FilePath: \passiflora-edulis-sims\src\components\MyLeaferCutom.ts
 * @Description: 创建一个 文字方框
 *
 * Copyright (c) 2025 by '1214391613@qq.com', All Rights Reserved.
 */
// #自定义元素 [继承 Rect]
import {
  registerUI,
  dataProcessor,
  Box,
  Text,
  dataType,
  BoxData,
} from '@leafer-ui/core' // 引入跨平台核心包
import type {
  IRectInputData,
  IRectData,
  ITextAlign,
  IVerticalAlign,
} from '@leafer-ui/interface'
import type { IStrokePaint } from '@leafer-ui/interface'
// 1. 定义数据类型
export interface IBoxTextInputData extends IRectInputData {
  text?: string
  font_size?: number
  text_align?: ITextAlign
  vertical_align?: IVerticalAlign
  text_color?: string
  font_name?: string
}

export interface IBoxTextData extends IRectData {
  text?: string
  font_size?: number
  text_color?: string
  font_name?: string
  text_align?: ITextAlign
  vertical_align?: IVerticalAlign
}

// 2. 创建数据类
export class BoxTextData extends BoxData implements IBoxTextData {}

// 3. 注册自定义组件
@registerUI()
class BoxText extends Box {
  public override get __tag() {
    return 'BoxText'
  }

  @dataProcessor(BoxTextData)
  public declare __: IBoxTextData

  // 设置默认值
  @dataType('')
  public declare text: string // 增加自定义属性， 注意必须加上 declare 关键词

  @dataType('#000000')
  public declare text_color: string

  @dataType(100)
  public declare font_size: number

  @dataType('center')
  public declare text_align: ITextAlign

  @dataType('middle')
  public declare vertical_align: IVerticalAlign

  @dataType('')
  public declare font_name: string

  constructor(data: IBoxTextInputData) {
    super(data)
    this.initText()
  }

  private initText() {
    const text_w = this.width != undefined ? this.width : 0
    const text_h = this.height != undefined ? this.height : 0

    const stroke = this.stroke as IStrokePaint
    const strokeWidth = stroke.style?.strokeWidth
    let padding = [0, 0]
    if (strokeWidth !== undefined) {
      padding = [strokeWidth as number, strokeWidth as number]
    }
    let text = undefined
    if (this.font_name === '') {
      text = Text.one({
        width: text_w,
        height: text_h,
        text: this.text,
        editable: true,
        fill: this.text_color,
        fontSize: this.font_size,
        textAlign: this.text_align,
        verticalAlign: this.vertical_align,
        padding: padding,
      })
    } else {
      text = Text.one({
        width: text_w,
        height: text_h,
        text: this.text,
        editable: true,
        fontFamily: this.font_name,
        fill: this.text_color,
        fontSize: this.font_size,
        textAlign: this.text_align,
        verticalAlign: this.vertical_align,
        padding: padding,
      })
    }

    this.add(text)
  }
}

export { BoxText }
